
$(function() {
	
	Tip.emp('员工');
	
	$("select[id^='emp_t_s']:visible").change(function() {
		var label = $("label[id^='label_emp_t']:visible");
		var a = $(this).val();
		if (a == 'emp') {
			label.html("员工：");
		} else if (a == 'oper') {
			label.html("录入员：");
		}
	});
});

var Plan = {
	dialog_add_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"添加": function() {
				if (!confirm("确认是否添加？")) {
					return false;
				}
				
				var form = $("#form_add")[0];

				var body = { };
				body.emp = form.emp.value;
				body.t1 = form.date1.value + " " + form.time1.value;
				body.t2 = form.date2.value + " " + form.time2.value;

				body.content = form.content.value;

				$(this).dialog("close");

				$.post('kq.php?do=Plan.addPost', body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_add(url, title, xy);
	},

	dialog_cal_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"计算": function() {
				if (!confirm("确认是否开始计算？")) {
					return false;
				}
				
				var form = $("#form_add")[0];

				var body = { };
				body.emp = form.emp.value;
				body.date1 = form.date1.value;
				body.date2 = form.date2.value;

				body.content = form.content.value;

				$(this).dialog("close");

				$.post('kq.php?do=Plan.calPost', body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_add(url, title, xy);
	},

	dialog_batch_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭" : function() {
				$(this).dialog("close");
			},

			"生成" : function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_add")[0];

				var body = {};
				body.emp = form.emp.value;

				body.date1 = form.date1.value;
				body.date2 = form.date2.value;

				body.time1 = form.time1.value;
				body.time2 = form.time2.value;
				
				var week = new Array();
				$("input[name^='week_']").each(function() {
					if ($(this).attr("checked")) {
						week.push($(this).val());
					}
				})
				
				// alert($.toJSON(week));
				body.week = $.toJSON(week);

				body.holiday = form.holiday.value;
				body.content = form.content.value;

				$(this).dialog("close");

				$.post('kq.php?do=Plan.batchPost', body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_add(url, title, xy);
	},

	dialog_dept_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_edit').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"更新": function() {
				if (!confirm("确认是否开始提交？")) {
					return false;
				}
				
				var form = $("#form_edit")[0];

				var body = { };
				body.t1 = form.t1.value;
				body.t2 = form.t2.value;

				$(this).dialog("close");

				$.post('kq.php?do=Plan.deptPost', body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_edit(url, title, xy);
	},	

	popup: function (e, p2, id) {
		$.cookie('id_', id);

		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'kq.php?do=Plan.edit&id=' + id + p2;
		var c2 = 'kq.php?do=Plan.view&id=' + id;
		var c3 = 'kq.php?do=Plan.copy&id=' + id;
			
		var a1 = ['编辑信息', 'url', 'Plan.dialog_edit_', '650x400'];
		var a2 = ['查看信息', 'url', 'Common.dialog_view_', '650x380'];
		var a3 = ['复制信息', 'url', 'Plan.dialog_copy_', '650x320'];
		
		var s = c1 + ", " + c2 + ", " + c3;
		
		return popup(e, [a1, a2, a3], s);
	},

	/**
	 * 编辑
	 */
	dialog_edit_: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('kq.php?do=Plan.ajax_oper', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"修改": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						body.emp = form.emp.value;
						body.t1 = form.date1.value + " " + form.time1.value;
						body.t2 = form.date2.value + " " + form.time2.value;

						body.content = form.content.value;
						body.status = form.status.value;

						$(this).dialog("close");

						var url = 'kq.php?do=Plan.editPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},

	/**
	 * 复制
	 */
	dialog_copy_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"复制": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_add")[0];

				var body = {};
				body.emp = form.emp.value;
				body.t1 = form.date1.value + " " + form.time1.value;
				body.t2 = form.date2.value + " " + form.time2.value;

				body.content = form.content.value;

				$(this).dialog("close");

				var url = 'kq.php?do=Plan.addPost';

				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_add(url, title, xy);
	},
	
	popup_all: function (e, p2) {
		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'kq.php?do=Plan.delete' + p2;	
		var c9 = 'kq.php?do=Plan.xls' + p2;	
			
		var a1 = ['删除信息', 'url', 'Common.list_delete'];				
		var a9 = ['导出Excel', 'url', 'jswin', '1024x768'];

		var s = c1 + ", " + c9;

		return popup(e, [a1, a9], s);
	}
};
